﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace 省市选择
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          /*  Person p1 = new Person();
            p1.Age = 11;
            p1.Name = "lucy";

            Person p2 = new Person();
            p2.Age = 18;
            p2.Name = "kate";

            comboBox1.Items.Add(p1);
            comboBox1.Items.Add(p2);*/

            string connStr =  ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from T_province";
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            ProvinceItem item = new ProvinceItem();
                            item.Id = reader.GetInt32(reader.GetOrdinal("proID"));
                            item.Name = reader.GetString(reader.GetOrdinal("proName"));
                            comboBox1.Items.Add(item);
                        }
                    }
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ProvinceItem item = (ProvinceItem)comboBox1.SelectedItem;
            int id = item.Id;
            string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using(SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from T_city where proID = @proId";
                    cmd.Parameters.Add(new SqlParameter("proId", id));
                    comboBox2.Items.Clear();
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string cityName = reader.GetString(reader.GetOrdinal("cityName"));
                            comboBox2.Items.Add(cityName);
                        }
                    }
                }
            }
        }
    }

    class ProvinceItem 
    {
        public string Name { get; set; }
        public int Id { get; set; }
    }


    /*class Person
    {
        public string Name{
            get;
            set;
        }
        public int Age{
            get;
            set;
        }

        public override string ToString()
        {
            return Convert.ToString(Age);
        }
    }*/
}
